package com.frxuexi.park2209.repair

import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.frxuexi.base.mvvm.BaseActivity
import com.frxuexi.base.net.Const
import com.frxuexi.park2209.R
import com.frxuexi.park2209.databinding.ActivityAddRepairBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus

@AndroidEntryPoint
class AddRepairActivity : BaseActivity<ActivityAddRepairBinding,RepairViewModel>() {
    override fun initData() {
        lifecycleScope.launch {
            vm.addRepair.collect{
                if(it.isNotEmpty()) {
                    ToastUtils.showLong("提交成功")
                    EventBus.getDefault().post("aa")
                    finish()
                }
            }
        }
    }

    override fun initView() {
        binding.addrepSub.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val length = s?.length
                binding.addrepSum.text="${length}/10"
                if(length==10){
                    binding.addrepSum.setTextColor(Color.RED)
                }else{
                    binding.addrepSum.setTextColor(Color.GRAY)
                }
            }

            override fun afterTextChanged(s: Editable?) {

            }

        })

        binding.addrepImg.setOnClickListener {
            PictureSelector.create(this@AddRepairActivity,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }

        binding.btnAddrep.setOnClickListener {
            /*{
                "departmentId": 0,
                "repairData": "string",
                "repairDescribe": "string",
                "repairId": 0,
                "repairTime": "string",
                "repairUserId": 0,
                "userId": 0
            }*/
            val map = mutableMapOf<String, Any>()
            map["userId"]=SPUtils.getInstance().getInt(Const.UID,-1)
            map["repairData"]=binding.addrepData.text.toString()
            map["repairDescribe"]=binding.addrepSub.text.toString()
            vm.addRepair(map)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode== PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            val bean =
                data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            Glide.with(this).load(bean?.path).into(binding.addrepImg)
        }
    }
}